package com.thunder.rebot.rebotclient.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.icu.text.IDNA;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.limedia.explorer.centralcontrol.ControlActivity;
import com.thunder.rebot.rebotclient.R;
import com.thunder.rebot.rebotclient.adapter.DisplayListAdapter;
import com.thunder.rebot.rebotclient.entity.InfoData;
import com.thunder.rebot.rebotclient.entity.Network;
import com.thunder.rebot.rebotclient.service.ServiceClient;
import com.thunder.rebot.rebotclient.utils.ClientUtils;
import com.thunder.rebot.rebotclient.utils.LogUtil;
import com.thunder.rebot.rebotclient.utils.TRCommonUtil;
import com.thunder.rebot.rebotclient.view.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuechunan on 18-7-2.
 */

public class DisplayFaceActivity extends BaseActivity implements DisplayListAdapter.CheckItemListener {

    private static final String TAG = "DisplayFaceActivity";
    @BindView(R.id.center_txt)
    public TextView centerTxt;
    @BindView(R.id.left_img)
    public ImageView leftImg;
    @BindView(R.id.right_txt)
    public TextView rightTxt;
    @BindView(R.id.recyclerView)
    public RecyclerView recyclerView;
    @BindView(R.id.all_btn)
    public Button allBtn;
    @BindView(R.id.delete_btn)
    public Button deleteBtn;
    private DisplayListAdapter displayAdapter;
    private List<Map<String, Object>> mDatasSelect = new ArrayList<Map<String, Object>>();
    private List<InfoData> mDatas=new ArrayList<InfoData>();
    private List<InfoData> mDatas1=new ArrayList<InfoData>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_face);
        ServiceClient.setOnPackageRemoveListener(DisplayFaceActivity.this);
        ButterKnife.bind(this);
        centerTxt.setText(R.string.register_finish_title_txt);
        rightTxt.setText(R.string.main_right_txt);
        recyclerView.setLayoutManager(gridLayoutManager);
        displayAdapter = new DisplayListAdapter(this, mDatas,mDatasSelect, this);
        //设置item间距，16dp
        recyclerView.addItemDecoration(new SpaceItemDecoration(16));
        recyclerView.setAdapter(displayAdapter);
    }

    private void initDataSendService() {
        new Thread("send") {
            @Override
            public void run() {
                if (ClientUtils.connectFlag) {
                    new Thread("send") {
                        @Override
                        public void run() {
                            Network.SomeRequest request = new Network.SomeRequest();
                            request.sendType = 2;
                            ServiceClient.client.sendTCP(request);
                        }
                    }.start();
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(DisplayFaceActivity.this, "没有连接服务端，请连接", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //查询
        mDatas.clear();
        mDatasSelect.clear();
        displayAdapter.notificationList(mDatas,mDatasSelect);
        initDataSendService();
    }

    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3) {
        @Override
        public View onInterceptFocusSearch(View focused, int direction) {
            int fromPos = getPosition((View) focused.getTag());//当前焦点的位置
            if (direction == View.FOCUS_UP) {
                fromPos--;
            } else if (direction == View.FOCUS_DOWN) {
                fromPos++;
            }
            if (fromPos < 0 || fromPos >= getItemCount()) {
                return focused;
            }
            return super.onInterceptFocusSearch(focused, direction);
        }
    };

    @OnClick({R.id.left_img, R.id.right_txt, R.id.all_btn, R.id.delete_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_img:
                finish();
                break;
            case R.id.right_txt:
                Intent intentMain = new Intent(this, ControlActivity.class);
                startActivity(intentMain);
                finish();
                break;
            case R.id.all_btn:
                LogUtil.i(TAG,"mDatasSelect.size()=="+mDatasSelect.size());
                for (int i = 0; i < mDatas.size(); i++) {
                    mDatasSelect.get(i).put("check", true);
                    LogUtil.i(TAG,"mDatasSelect.size()==选择true");
                }
                displayAdapter.notificationList(mDatas,mDatasSelect);
                break;
            case R.id.delete_btn:
                LogUtil.i(TAG,"mDatasSelect.size()=="+mDatasSelect.size());
                LogUtil.i(TAG,"mDatas.size()=="+mDatas.size());
                for (int i = 0; i < mDatas.size(); i++) {
                    LogUtil.i(TAG,"mDatasSelect.size()=="+(boolean)mDatasSelect.get(i).get("check"));
                    if ((boolean) mDatasSelect.get(i).get("check")) {
                        //数据库删除
//                        infoData.deleteById(mDatas.get(i).getId());
                        final long sendId=mDatas.get(i).getId();
                        new Thread("send") {
                            public void run() {
                                Network.SomeRequest request = new Network.SomeRequest();
                                request.sendType = 3;
                                request.id=sendId;
                                LogUtil.d(TAG, "删除照片 id=="+sendId);
                                ServiceClient.client.sendTCP(request);
                            }
                        }.start();
                        //数据源删除
                        mDatas.remove(i);
                        mDatasSelect.remove(i);
                        displayAdapter.notificationList(mDatas,mDatasSelect);
                    }
                }
//                mDatas.clear();
//                mDatasSelect.clear();
//                mDatas=infoData.getDataAll();
//                for(int i=0;i<mDatas.size();i++){
//                    Map<String,Object> map = new HashMap<String,Object>();
////                map.put("bit", bm);
//                    map.put("path", mDatas.get(i).getPicPath());
//                    map.put("check", false);
//                    mDatasSelect.add(map);
//                }
//                displayAdapter.notificationList(mDatas,mDatasSelect);
                break;
        }
    }

    @Override
    public void itemChecked(int position, boolean isChecked) {
        //处理Item点击选中回调事件
        mDatasSelect.get(position).put("check", isChecked);
    }

    @Override
    public void getRebackPicByte(byte[] pisbyte,long id,int dataSize) {
        LogUtil.i(TAG,"getRebackPicByte value=="+Arrays.toString(pisbyte)+",,id=="+id+",,dataSize=="+dataSize);
//        Bitmap bitmap = TRCommonUtil.byteToBitmap(pisbyte);
        InfoData info=new InfoData(id,pisbyte);
        mDatas1.add(info);
        LogUtil.i(TAG,"getRebackPicByte vmDatas.size()=="+mDatas1.size()+mDatas.size());
//        if(mDatas1.size()==dataSize){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    LogUtil.i(TAG,"getRebackPicByte value==刷新");
                    for(int i=0;i<mDatas1.size();i++){
                        Map<String,Object> map = new HashMap<String,Object>();
//                map.put("bit", bm);
                        map.put("path", mDatas1.get(i).getId());
                        map.put("check", false);
                        mDatasSelect.add(map);
                    }
                    mDatas.addAll(mDatas1);
                    displayAdapter.notificationList(mDatas,mDatasSelect);
                    mDatas1.clear();
                }
            });
//        }
    }

}

